package leetcode.N1_N100;

import leetcode.comm.ListNode;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/13 8:44 下午
 */
public class N61 {

    public static ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0) {
            return head;
        }
        // 简单起见 先遍历head计算出数量
        ListNode current = head;
        int count = 0;
        while (current != null) {
            count++;
            current = current.next;
        }
        // 找到节点 讲链表切成两段
        int step = count - (k % count);
        if (count == step) {
            return head;
        }
        ListNode newTailNode = head;
        for (int i = 0; i < step; i++) {
            newTailNode = newTailNode.next;
        }
        ListNode newHead = newTailNode.next;
        newTailNode.next = null;
        ListNode newPoint = newHead;
        while (newPoint.next != null) {
            newPoint = newPoint.next;
        }
        newPoint.next = head;
        return newHead;
    }
}
